﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Nhom14_HTTTHD_BuyMe.function;

namespace Nhom14_HTTTHD_BuyMe
{
    /// <summary>
    /// Summary description for EntitiesCentric
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class EntitiesCentric : System.Web.Services.WebService
    {
        
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int AddAccount(string username, string password, string email, string firstname, string lastname, string tel, string fax, string company, string website)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    account a = new account();
                    a.username = username;
                    a.password = password;
                    a.email = email;
                    a.firstname = firstname;
                    a.lastname = lastname;
                    a.tel = tel;
                    a.fax = fax;
                    a.company = company;
                    a.website = website;
                    a.credit = 0;

                    context.accounts.AddObject(a);
                    context.SaveChanges();

                    return a.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int AddBidDomain(int id_account, int id_domain, float price, string message)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    bid_domain d = new bid_domain();
                    d.id_account = id_account;
                    d.id_domain = id_domain;
                    d.message = message;
                    d.price = price;
                    d.timestamp = DateTime.Now;

                    context.bid_domain.AddObject(d);
                    context.SaveChanges();
                    return d.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int AddBidWebsite(int id_account, int id_website, float price, string message)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    bid_website d = new bid_website();
                    d.id_account = id_account;
                    d.id_website = id_website;
                    d.message = message;
                    d.price = price;
                    d.timestamp = DateTime.Now;

                    context.bid_website.AddObject(d);
                    context.SaveChanges();
                    return d.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int AddMessage(int from_account, int to_account, string message)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    message obj = new message();
                    obj.id_from_username = from_account;
                    obj.id_to_username = to_account;
                    obj.message1 = message;
                    obj.timestamp = DateTime.Now;
                    obj.parent = 0;
                    context.messages.AddObject(obj);
                    context.SaveChanges();

                    return obj.id;
                }
            }
            catch
            {
                return 0;
            }
        }
        
        [WebMethod]
        public int AddWebsite(int id_account, int SiteStatus, string SiteName, string SiteURL, string Description, string Visitor, string PageView, string Income, string Cost, string Disk, String Bandwidth, string member, string price, int id_category)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    website obj = new website();
                    obj.id_account = id_account;
                    obj.id_website_status = SiteStatus;
                    obj.id_type = 2;
                    obj.timestamp = DateTime.Now;
                    obj.site_name = SiteName;
                    obj.site_url = SiteURL;
                    obj.description = Description;
                    obj.visitiors = Visitor;
                    obj.page_views = PageView;
                    obj.income = Income;
                    obj.cost_to_run = Cost;
                    obj.disk_space_used = Disk;
                    obj.bandwidth_used = Bandwidth;
                    obj.members = member;
                    obj.asking_price = price;
                    obj.date_started = DateTime.Now;
                    obj.stats_availabale = "0";
                    obj.status = 1;

                    category obj_c = new category();
                    obj_c.id = id_category;
                    obj.categories.Add(obj_c);

                    context.websites.AddObject(obj);
                    context.SaveChanges();

                    obj.image = "Images/" + obj.id + ".jpg";
                    context.SaveChanges();
                    return obj.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int AddDomain(int account, string siteURL, string description, string pageview, string price, int category)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    domain obj = new domain();
                    obj.id_account = account;
                    obj.id_type = 2;
                    obj.timestamp = DateTime.Now;
                    obj.domain_url = siteURL;
                    obj.description = description;
                    obj.page_view = pageview;
                    obj.asking_price = price;
                    obj.date_started = DateTime.Now;
                    obj.stats_available = "0";
                    obj.status = 1;

                    category obj_c = new category();
                    obj_c.id = category;
                    obj.categories.Add(obj_c);

                    context.domains.AddObject(obj);
                    context.SaveChanges();
                    return obj.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int UpdateDomainType(int id, int type)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var kq = from s in context.domains
                             where s.id == id
                             select s;

                    if (kq.Count() == 0)
                        return 0;

                    domain dmain = kq.First();

                    dmain = kq.First();
                    dmain.id_type = type;

                    context.SaveChanges();
                    return dmain.id;
                }
            }
            catch
            {
                return 0;
            }
        }
        
        [WebMethod]
        public int UpdateWebsiteType(int id, int type)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var kq = from s in context.websites
                             where s.id == id
                             select s;

                    if (kq.Count() == 0)
                        return 0;

                    website web = kq.First();

                    web = kq.First();
                    web.id_type = type;

                    context.SaveChanges();
                    return web.id;
                }
            }
            catch
            {
                return 0;
            }
        }
        
        [WebMethod]
        public int IncreaseAccountCredit(int id, int iMoney)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var accs = from x in context.accounts
                               where x.id == id
                               select x;

                    if (accs.Count() == 0)
                        return 0;

                    account acc = accs.FirstOrDefault();
                    acc.credit += iMoney;
                    context.SaveChanges();
                    return iMoney;
                }
            }
            catch
            {
                return 0;
            }
        }
         

        [WebMethod]
        public int DecreaseAccountCredit(int id, float iMoney)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var accs = from x in context.accounts
                               where x.id == id
                               select x;

                    if (accs.Count() == 0)
                        return 0;

                    account acc = accs.FirstOrDefault();
                    if (acc.credit >= iMoney)
                    {
                        acc.credit -= iMoney;
                        context.SaveChanges();
                        return 1;
                    }
                    else return 0;
                }
            }
            catch
            {
                return 0;
            }
        }
         

        [WebMethod]
        public int AddPayment(int from_account, int to_account, float money, string message)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    payment obj = new payment();
                    obj.id_from_username = from_account;
                    obj.id_to_username = to_account;
                    obj.message = message;
                    obj.money = money;
                    context.payments.AddObject(obj);
                    context.SaveChanges();
                    return obj.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int AddTransaction(int bidID, int type, float price)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    transaction t = new transaction();

                    t.id_bid = bidID;
                    t.type_bid = type;
                    t.total = price;
                    t.timestamp = DateTime.Now;
                    context.transactions.AddObject(t);
                    context.SaveChanges();

                    return t.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int GetWebsiteAccountID(int id)
        {
            try
            {
                websitebrokenEntities context = new websitebrokenEntities();

                var result = from x in context.websites
                             where x.id == id
                             select x;

                if (result.Count() == 0)
                    return 0;

                website web = result.First();

                return (int)web.id_account;
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int GetDomainAccountID(int id)
        {
            try
            {
                websitebrokenEntities context = new websitebrokenEntities();

                var result = from x in context.websites
                             where x.id == id
                             select x;

                if (result.Count() == 0)
                    return 0;

                website web = result.First();

                return (int)web.id_account;
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int UpdateTransactionInformation(int id, string Information)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.transactions
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    transaction t = result.First();

                    t.information = Information;
                    t.status = "2";
                    context.SaveChanges();
                    return t.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int CloseDomain(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var kq = from s in context.domains
                             where s.id == id
                             select s;

                    if (kq.Count() == 0)
                        return 0;

                    domain dmain = kq.First();

                    dmain = kq.First();
                    dmain.status = 0;

                    context.SaveChanges();
                    return dmain.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int CloseWebsite(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var kq = from s in context.websites
                             where s.id == id
                             select s;

                    if (kq.Count() == 0)
                        return 0;

                    website web = kq.First();

                    web = kq.First();
                    web.status = 0;

                    context.SaveChanges();
                    return web.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public float GetBidDomainPrice(int bidID)
        {
            float price = 0;
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    //Use BidWebsite.GetPrice
                    var result = from x in context.bid_domain
                                 where x.id == bidID
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    bid_domain bid = result.First();

                    price = (float)(bid.price);
                    return price;
                }
            }
            catch
            {
                return 0;
            }
        }
        
        [WebMethod]
        public float GetBidWebsitePrice(int bidID)
        {
            float price = 0;
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    //Use BidWebsite.GetPrice
                    var result = from x in context.bid_website
                                 where x.id == bidID
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    bid_website bid = result.First();

                    price = (float)(bid.price);
                    return price;
                }
            }
            catch
            {
                return 0;
            }
        }
         

        [WebMethod]
        public int GetBidWebsiteAccount(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.bid_website
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    bid_website bid = result.First();

                    return (int)bid.id_account;
                }
            }
            catch
            {
                return 0;
            }
        }
        
        [WebMethod]
        public int GetBidDomainAccount(int bidID)
        {
            float price = 0;
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    //Use BidWebsite.GetPrice
                    var result = from x in context.bid_domain
                                 where x.id == bidID
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    bid_domain bid = result.First();

                    return (int)bid.id_account;
                }
            }
            catch
            {
                return 0;
            }
        }
         
        [WebMethod]
        public int GetBuyer(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.transactions
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    transaction t = result.First();

                    if (t.type_bid == 1)
                    {
                        return BidWebsite.GetAccountBid((int)t.id_bid);
                    }
                    else return BidDomain.GetAccountBid((int)t.id_bid);
                }
            }
            catch
            {
                return 0;
            }
        }
        [WebMethod]
        public int ChangeStatus(int id, string ss)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.transactions
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    transaction t = result.First();

                    t.status = ss;
                    context.SaveChanges();
                    return 1;
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int GetSeller(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.transactions
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    transaction t = result.First();

                    if (t.type_bid == 1)
                    {
                        int w = BidWebsite.GetWebsiteID((int)t.id_bid);
                        return Website.GetAccountID(w);
                    }
                    else
                    {
                        int d = BidDomain.GetDomainID((int)t.id_bid);
                        return Domain.GetAccountID(d);
                    }
                }
            }
            catch
            {
                return 0;
            }
        }

        [WebMethod]
        public int ChoosePaymentType(int iTranID, int type_w)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    transaction t = (from x in context.transactions
                                     where x.id == iTranID
                                     select x).FirstOrDefault();
                    if (t != null)
                    {
                        t.payment_type = 1;
                        t.status = "1";
                        if (type_w == 2)
                        {
                            t.payment_type = 2;
                            t.status = "3";
                        }
                        context.SaveChanges();
                    }
                    else
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
    }
        
}
